/**
 * 
 */
package com.hongye.app.model;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.hongye.app.controller.UserAppInfoController;
import com.hongye.util.Utils;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 商品评价
 * @author changpf at 2016年9月15日21:48:35
 *
 */
public class EvaluationModel extends Model<EvaluationModel> {
	
	private static final long serialVersionUID = -3178808507706403714L;
	
	public static final EvaluationModel dao=new EvaluationModel();
	
	/**
	 * 得到商品评价  第一页的评论
	 * @param gid
	 * @param pageNumber
	 * @return
	 */
	public List<EvaluationModel> getEvaluById(int gid){
		String sql_select = "SELECT a.user_id, a.evaluation, a.thumb1, a.thumb2, a.thumb3, a.thumb4, a.c_time";
		String sql_where = "FROM evaluation a WHERE a.gid = "+ gid +"";
		Page<EvaluationModel> goods_list = dao.paginate(1, 50, sql_select, sql_where);//TODO  详情页面完成后改成 Utils.pageSize
		return getEvaluationPageNumber(goods_list.getList());
	}		
	
	/**
	 * 根据商家id获取 该商家的所有评论的数量
	 * @author liushaomin
	 * 2016年12月16日
	 * @param seller_id
	 * @return
	 */
	public static List<EvaluationModel> getEvaluBySeller_id(String seller_id){
		String sql="SELECT * FROM evaluation WHERE seller_id = "+seller_id+" group by order_id";
		List<EvaluationModel> evaluation = EvaluationModel.dao.find(sql);
		return evaluation;
	}
	
	/**
	 * 得到某项（key）评分的平均值
	 * @author liushaomin
	 * 2017年5月23日  
	 * @param key 求平均的字段
	 * @param sellerid 用户id
	 * @return
	 */
	public static double average(String key, int sellerid){
		String sql = "SELECT TRUNCATE(AVG("+ key +"),1) as liu FROM evaluation WHERE seller_id = "+ sellerid +" GROUP BY order_id";
		EvaluationModel evaluation = dao.findFirst(sql);
		if(evaluation == null || evaluation.get("liu") == null){
			return 0.0;
		}
		return evaluation.getBigDecimal("liu").doubleValue();
	}
	
	/**
	 * 得到当前时间的前某个（num_month）月的总评分的平均值
	 * @author liushaomin
	 * 2017年5月23日
	 * @param sellerid
	 * @param num_month 当前时间的前某个月，例如当前是五月，想要获取三月份的平均值，则num_month为1（从0开始）;num_month为-1时，没有时间限制
	 * @return
	 */
	public static double average_credit(int sellerid, int num_month){
//		String sql = "SELECT * FROM evaluation WHERE seller_id = "+ sellerid +" AND  DATE_FORMAT(c_time,'%Y-%m') = DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL "+num_month+" MONTH),'%Y-%m')";
		String sql = "SELECT * FROM evaluation WHERE seller_id = "+ sellerid +" AND DATE_FORMAT(c_time,'%Y-%m') < DATE_FORMAT(DATE_SUB(now(),INTERVAL "+ num_month +" MONTH), '%Y-%m') GROUP BY order_id";
		List<EvaluationModel> models = dao.find(sql);
		if(models.size() > 0){
			double sum = 0;
			for (EvaluationModel item : models) {
				double itemsum = Utils.add(Utils.add(Utils.muti(item.getInt("service_level"), 0.25), Utils.muti(item.getInt("logistic_level"), 0.25)), Utils.muti(item.getInt("goods_level"), 0.5));
				sum = Utils.add(sum, itemsum);
			}
			return Utils.div(sum, models.size(), 1);
		}else{
			return 0.0; 
		}
	}
	
		
	/**
	 * 评分折线图数据——获取当前时间的前number个月份
	 * @author liushaomin
	 * 2017年5月23日
	 * @param number 
	 * @return
	 */
	public static Map<String, Object> linechart(int number, int sellerid){
		Map<String, Object> linechart = new HashMap<String, Object>();
		Map<String, Object> monthmap = new HashMap<String, Object>();
		Map<String, Object> spot = new HashMap<String, Object>();
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < number; i++) {
			if(i==0){
				calendar.add(Calendar.MONTH, i);
			}else{
				calendar.add(Calendar.MONTH, -1);
			}
			int month = calendar.get(Calendar.MONTH);
			monthmap.put("month_" + i, month);
			spot.put("month_"+ i +"_spot", average_credit(sellerid, i));
		}
		linechart.put("month", monthmap);
		linechart.put("spot", spot);
		return linechart;
	}

	/**
	 * 获取商品的分页评论
	 * @author liushaomin
	 * 2017年6月24日
	 * @param goodsid 商品id
	 * @param pageNumber 页码
	 * @return
	 */
	public static Page<EvaluationModel> getEvaluationPageNumber(int goodsid, int pageNumber) {
		String sql_select = "SELECT a.user_id, a.evaluation, a.thumb1, a.thumb2, a.thumb3, a.thumb4, a.c_time";
		String sql_where = "FROM evaluation a WHERE a.gid = "+ goodsid +"";
		Page<EvaluationModel> goods_list = dao.paginate(pageNumber, 1, sql_select, sql_where);//TODO  详情页面完成后改成 Utils.pageSize
		getEvaluationPageNumber(goods_list.getList());
		return goods_list;
	}
	
	/**
	 * 评论中添加用户信息
	 * @author liushaomin
	 * 2017年6月24日
	 * @param evaluation
	 * @return
	 */
	public static List<EvaluationModel> getEvaluationPageNumber(List<EvaluationModel> evaluationlist) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
		for (EvaluationModel evaluation : evaluationlist) {
			Date ts = evaluation.get("c_time");
			evaluation.put("c_time", Utils.dateTosdf(Utils.sdfToDate(ts.toString(), df), df2));
			if(evaluation.getStr("user_id").equals("-1")){
				evaluation.put("avatar", UserAppInfoController.ImgPath + "defaultavatar.png");
				evaluation.put("nick", "匿名");
			}else{
				UserAppModel user = UserAppModel.getById(evaluation.getStr("user_id"));
				evaluation.put("avatar", UserAppInfoController.ImgPath + user.getStr("avatar")); 
				evaluation.put("nick", user.getStr("nick"));
			}
			if(StringUtils.isEmpty(evaluation.getStr("thumb1"))){
				evaluation.put("thumb1", "true");
			}else{
				evaluation.put("thumb1", UserAppInfoController.ImgPath + evaluation.get("thumb1"));
			}
			if(StringUtils.isEmpty(evaluation.getStr("thumb2"))){
				evaluation.put("thumb2", "true");
			}else{
				evaluation.put("thumb2", UserAppInfoController.ImgPath + evaluation.get("thumb2"));
			}
			if(StringUtils.isEmpty(evaluation.getStr("thumb3"))){
				evaluation.put("thumb3", "true");
			}else{
				evaluation.put("thumb3", UserAppInfoController.ImgPath + evaluation.get("thumb3"));
			}
			if(StringUtils.isEmpty(evaluation.getStr("thumb4"))){
				evaluation.put("thumb4", "true");
			}else{
				evaluation.put("thumb4", UserAppInfoController.ImgPath + evaluation.get("thumb4"));
			}
		}
		return evaluationlist;
	}
	
}
